package com.cloud.system.controller;

import com.github.pagehelper.PageHelper;
import com.cloud.common.constants.ExcelPoiConstants;
import com.cloud.common.domain.Result;
import com.cloud.controller.BaseController;
import com.cloud.enums.BusinessType;
import com.cloud.common.enums.CheckType;
import com.cloud.logs.Logs;
import com.cloud.security.aop.RequestPermissions;
import com.cloud.system.entity.SysConfig;
import com.cloud.system.service.ISysConfigService;
import com.cloud.utils.ExcelPoiUtils;
import com.cloud.utils.SecurityUtils;
import com.cloud.utils.ServletUtils;
import com.cloud.utils.ToolUtils;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 参数配置 信息操作处理
 *
 * @author hhb, yzj
 */
@Tag(name = "配置管理")
@RestController
@RequestMapping("/system/config")
public class SysConfigController extends BaseController {

    @Autowired
    private ISysConfigService configService;

    @Operation(summary = "获取参数配置列表")
    @RequestPermissions("system:config:list")
    @GetMapping("/list")
    public Result list(SysConfig config) {
        startPage();
        List<SysConfig> list = configService.selectConfigList(config);
        return getDataTable(list);
    }

    @Operation(summary = "导出参数配置列表")
    @Logs(title = "导出参数管理", businessType = BusinessType.EXPORT)
    @RequestPermissions("system:config:export")
    @GetMapping("/export")
    public void export(SysConfig config) {
        PageHelper.startPage(ExcelPoiConstants.startNum, ExcelPoiConstants.maxSize);
        List<SysConfig> list = configService.selectConfigList(config);
        String titleName = "系统配置列表";
        ExcelPoiUtils.exportExcel(list, titleName, "sys_config_info",
                SysConfig.class, titleName + ".xls", ServletUtils.getResponse());
    }

    @Operation(summary = "根据参数编号获取详细信息")
    @Parameter(name = "configId", description = "参数编号")
    @RequestPermissions("system:config:query")
    @GetMapping(value = "/getInfo")
    public Result getInfo(Long configId) {
        return Result.success("查询成功", configService.selectConfigById(configId));
    }

    @Operation(summary = "根据参数键名查询参数值")
    @Parameter(name = "configKey", description = "参数键名")
    @GetMapping(value = "/configKey/getConfigKey")
    public Result getConfigKey(String configKey) {
        return Result.success("查询成功", configService.selectConfigByKey(configKey));
    }

    @Operation(summary = "新增参数配置")
    @Logs(title = "新增参数管理", businessType = BusinessType.INSERT)
    @RequestPermissions("system:config:add")
    @PostMapping("/insert")
    public Result insert(@RequestBody SysConfig config) {
        if (CheckType.NOT_UNIQUE.getCode().equals(configService.checkConfigKeyUnique(config))) {
            return Result.error("新增参数[" + config.getConfigName() + "]失败,参数键名已存在");
        }
        return toResult(configService.insertConfig(config));
    }

    @Operation(summary = "修改参数配置")
    @Logs(title = "修改参数管理", businessType = BusinessType.UPDATE)
    @RequestPermissions("system:config:edit")
    @PostMapping("/update")
    public Result update(@RequestBody SysConfig config) {
        if (CheckType.NOT_UNIQUE.getCode().equals(configService.checkConfigKeyUnique(config))) {
            return Result.error("修改参数[" + config.getConfigName() + "]失败,参数键名已存在");
        }
        config.setUpdateBy(SecurityUtils.getUserName());
        return toResult(configService.updateConfig(config));
    }

    @Operation(summary = "删除参数配置")
    @Parameter(name = "configIds", description = "参数编号[多个用,隔开]")
    @Logs(title = "删除参数管理", businessType = BusinessType.DELETE)
    @RequestPermissions("system:config:remove")
    @PostMapping("/remove")
    public Result remove(String configIds) {
        List<Long> list = ToolUtils.setIdsToList(configIds);
        return toResult(configService.deleteConfigByIds(list));
    }

    @Operation(summary = "刷新参数缓存")
    @Logs(title = "刷新参数管理", businessType = BusinessType.CLEAN)
    @RequestPermissions("system:config:remove")
    @GetMapping("/clearCache")
    public Result clearCache() {
        configService.clearCache();
        return Result.success("操作成功");
    }

    @Operation(summary = "Feign内部调用(查询配置参数)")
    @RequestMapping(value = "/inAuth/getInConfig", method = {RequestMethod.POST, RequestMethod.GET})
    public Result getInConfig(String configKey) {
        return Result.success("查询成功", configService.selectConfigByKey(configKey));
    }

}
